<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
      label-position="left"
    >
      <el-form-item label="表名称" prop="tableName">
        <el-input v-model="formData.tableName" disabled placeholder="请输入表名称" />
      </el-form-item>
      <el-form-item label="字段命名" prop="columnName">
        <el-input v-model="formData.columnName" placeholder="只允许小写英文与下划线，例如task_field" />
      </el-form-item>
      <el-form-item label="字段标签" prop="columnDescribe">
        <el-input v-model="formData.columnDescribe" placeholder="字段标签将作为业务表的表头显示" />
      </el-form-item>
      <el-form-item label="字段长度" prop="columnLength">
        <el-input v-model="formData.columnLength" placeholder="请填写字段长度" />
      </el-form-item>
      <el-form-item label="表格内编辑" prop="columnEdit">
<!--        <el-input v-model="formData.columnEdit" placeholder="是否可以在表格内编辑" />-->
        <el-radio-group v-model="formData.columnEdit">
          <el-radio
            v-for="dict in status"
            :key="dict.value"
            :value="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="js">
import { TableInfoApi } from '@/api/task/tableinfo'
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";

/** 字段信息 表单 */
defineOptions({ name: 'TableInfoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  tableName: undefined,
  columnName: undefined,
  columnDescribe: undefined,
  columnLength: undefined,
  columnEdit: undefined,
})
const formRules = reactive({
  tableName: [{ required: true, message: '表名称不能为空', trigger: 'blur' }],
  columnName: [{ required: true, message: '字段命名不能为空', trigger: 'blur' }],
  columnDescribe: [{ required: true, message: '字段标签不能为空', trigger: 'blur' }],
  columnLength: [{ required: true, message: '字段长度不能为空', trigger: 'blur' }],
  columnEdit: [{ required: true, message: '是否可在表格内编辑不得为空', trigger: 'blur' }],
})
const status = [
  {label:'开启',value:1},{label:'禁止',value:0}
]
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type,tableName, id) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await TableInfoApi.getTableInfo(id)
    } finally {
      formLoading.value = false
    }
  }
  console.log("tableName",tableName)
  formData.value.tableName = tableName
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value
    if (formType.value === 'create') {
      await TableInfoApi.createTableInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await TableInfoApi.updateTableInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    tableName: undefined,
    columnName: undefined,
    columnDescribe: undefined,
    columnType: undefined,
    orginal: undefined,
    required: undefined,
    selectType: undefined,
    enableInsert: undefined,
    enableUpdate: undefined,
    status: undefined,
    sort: undefined
  }
  formRef.value?.resetFields()
}
</script>
